﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SmartSpace.Silverlight
{
    public static class TextBoxMenuService
    {
        public static readonly DependencyProperty IsShowMenuProperty = DependencyProperty.RegisterAttached("IsShowMenu", typeof(bool), typeof(TextBox), new PropertyMetadata(false)
);
        public static void SetIsShowMenu(TextBox element, Boolean value)
        {
            if (value == true)
            {
                SetMenu(element);
            }
            element.SetValue(IsShowMenuProperty, value);
        }

        public static Boolean GetIsShowMenu(TextBox element)
        {
            return (Boolean)element.GetValue(IsShowMenuProperty);
        }

        private static void OnCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBox textBox = d as TextBox;

            SetMenu(textBox);
        }

        public static void SetMenu(this TextBox textBox)
        {
            ContextMenu menu = new ContextMenu();

            MenuItem menuCut = new MenuItem();
            menuCut.Header = "剪切";
            Image iconCut = new Image();
            iconCut.Source = new BitmapImage(new Uri("/SmartSpace.Silverlight;component/Control/Images/Cut.png", UriKind.Relative));
            iconCut.Stretch = Stretch.None;
            menuCut.Icon = iconCut;
            menuCut.Click += new RoutedEventHandler(DoCut);
            menuCut.Tag = textBox;
            menu.Items.Add(menuCut);

            MenuItem menuCopy = new MenuItem();
            menuCopy.Header = "复制";
            Image iconCopy = new Image();
            iconCopy.Source = new BitmapImage(new Uri("/SmartSpace.Silverlight;component/Control/Images/Copy.png", UriKind.Relative));
            iconCopy.Stretch = Stretch.None;
            menuCopy.Icon = iconCopy;
            menuCopy.Click += new RoutedEventHandler(DoCopy);
            menuCopy.Tag = textBox;
            menu.Items.Add(menuCopy);

            MenuItem menuPaste = new MenuItem();
            menuPaste.Header = "粘贴";
            Image iconPast = new Image();
            iconPast.Source = new BitmapImage(new Uri("/SmartSpace.Silverlight;component/Control/Images/Paste.png", UriKind.Relative));
            iconPast.Stretch = Stretch.None;
            menuPaste.Icon = iconPast;
            menuPaste.Click += new RoutedEventHandler(DoPaste);
            menuPaste.Tag = textBox;
            menu.Items.Add(menuPaste);

            menu.Opened += new RoutedEventHandler(menu_Opened);
            menu.Tag = textBox;

            ContextMenuService.SetContextMenu(textBox, menu);
        }

        static void menu_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu menu = sender as ContextMenu;
            TextBox textBox = menu.Tag as TextBox;
            if (string.IsNullOrEmpty(Clipboard.GetText()))
            {
                // 没有可以粘贴的内容
                (menu.Items[2] as MenuItem).IsEnabled = false;
            }
            else
            {
                (menu.Items[2] as MenuItem).IsEnabled = true;
            }
        }

        static void DoPaste(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            TextBox textBox = menuItem.Tag as TextBox;

            textBox.SelectedText = Clipboard.GetText();
        }

        static void DoCut(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            TextBox textBox = menuItem.Tag as TextBox;

            Clipboard.SetText(textBox.SelectedText);
            textBox.SelectedText = string.Empty;
            textBox.Focus();
        }

        static void DoCopy(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            TextBox textBox = menuItem.Tag as TextBox;

            Clipboard.SetText(textBox.SelectedText);
            textBox.Focus();
        }
    }
}
